###Appendix Material for Ideological Distance Between States###


#setwd("C:/Users/Josh Jackson/Dropbox/Spatial Paper with Jamie/Round 2 Replication")


####Forest Plot Results####
#Cross-Sectional Results
#EWM RESULTS:
#postscript("ewmForest.eps",horizontal=FALSE, width=5, height=5, paper='special', onefile=FALSE, pointsize=10)
#pdf("ewmForest.pdf",width=5, height=5,  pointsize=10)
lin.mean<-c(.7626, .8414, -1.023, .1294, -.2245, .7866, 1.053, .6601, .4428, -.3308, .4775)
lin.lower<-c(.5964, .5443, -1.32, -.1139, -.4867, .6171, .97, .5772, .2218, -.4606, .2508)
lin.upper<-c(.9294, 1.138, -.7259, .372, .03814, .9564, 1.136, .7431, .6647, -.2013, .7049)

car.mean<-c(0.6033, 0.8221,-0.8485,0.06182,-0.1247,0.6976,1.066,0.6652,0.3687,-0.2959,0.4933)
car.lower<-c(0.4052,0.4937,-1.215,-0.2279,-0.4378,0.5007,0.9106,0.5215,0.09225,-0.4948,0.2178)
car.upper<-c(0.8015,1.152,-0.4791,0.3506,0.1898,0.8948,1.221,0.8088,0.6458,-0.0954,0.7687)

ideol.mean<- c(.7479, .7642, -1.089, .1008, -.2855, .7607, 1.043, .6421, .4436, -.3225, .4768)
ideol.lower<-c( .626, .5559, -1.304, -.047, -.4499, .6543, .9953, .5966, .3191, -.4033, .3483)
ideol.upper<-c(.8685, .9711, -.8788, .2479, -.1243, .8676, 1.089, .6873, .5683, -.2416, .6052)


abs(lin.upper-lin.lower)
abs(car.upper-car.lower)
abs(ideol.upper-ideol.lower)
 
#Ideological Distance CAR Forest Plot
par(omi=c(.2,1.1,.1,.01))
plot(x=lin.mean,y=c(1:11-.1),xlim=c(-1.3,1.3),ylim=c(0,12), xlab="Coefficient", ylab="",axes=F)
axis(1)
axis(2, at=c(1:11), labels=c('Opinion on party elite','Opinion on Dem ID','Party elite on Dem ID','Opinion on strength','Party elite on strength','Dem ID on strength','Party elite on legis lib','Strength on legis lib','Opinion on policy','Strength on policy','Legis lib on policy'),las=1)
box()
abline(v=0,col='gray60')
segments(x0=lin.lower,x1=lin.upper,y0=c(1:11)-.1,y1=c(1:11)-.1, lty=2)
points(x=ideol.mean,y=c(1:11+.1), col='blue',pch=2)
segments(x0=ideol.lower,x1=ideol.upper,y0=c(1:11+.1),y1=c(1:11+.1), col='blue')
legend(x=-1.3, y=2.5, pch=c(2,1), lty=c(1,2), col=c('blue', 'black'), legend=c('CAR (Ideol)', 'Independent'))
#dev.off()


#Geographical Distance CAR Forest Plot
par(omi=c(.2,1.1,.1,.01))
plot(x=lin.mean,y=c(1:11-.1),xlim=c(-1.3,1.3),ylim=c(0,12), xlab="Coefficient", ylab="",axes=F)
axis(1)
axis(2, at=c(1:11), labels=c('Opinion on party elite','Opinion on Dem ID','Party elite on Dem ID','Opinion on strength','Party elite on strength','Dem ID on strength','Party elite on legis lib','Strength on legis lib','Opinion on policy','Strength on policy','Legis lib on policy'),las=1)
box()
abline(v=0,col='gray60')
segments(x0=lin.lower,x1=lin.upper,y0=c(1:11)-.1,y1=c(1:11)-.1, lty=2)
points(x=car.mean,y=c(1:11+.1), col='red',pch=2)
segments(x0=car.lower,x1=car.upper,y0=c(1:11+.1),y1=c(1:11+.1), col='red')
legend(x=-1.3, y=2.5, pch=c(2,1), lty=c(1,2), col=c('red', 'black'), legend=c('CAR', 'Independent'))


##convergence tests
library(coda)

#linear convergence test
a<-read.coda(output.file='ewmlinearchain1.txt', index.file='ewmlinearindex.txt', quiet=TRUE)
b<-read.coda(output.file='ewmlinearchain2.txt', index.file='ewmlinearindex.txt', quiet=TRUE)
c<-read.coda(output.file='ewmlinearchain3.txt', index.file='ewmlinearindex.txt', quiet=TRUE)
d<-mcmc.list(a,b,c)


#Assess Diagnostics
#Gelman-Rubin, requires multiple chains
#Approximate convergence diagnosed when upper limit is close to 1.
gelman.diag(d)
gelman.plot(d, ask=TRUE)


#CAR (ideology) convergence tests 
a<-read.coda(output.file='ewmideolcarchain1.txt', index.file='ewmideolcarindex.txt', quiet=TRUE)
b<-read.coda(output.file='ewmideolcarchain2.txt', index.file='ewmideolcarindex.txt', quiet=TRUE)
c<-read.coda(output.file='ewmideolcarchain3.txt', index.file='ewmideolcarindex.txt', quiet=TRUE)
d<-mcmc.list(a,b,c)

#Assess Diagnostics
#Gelman-Rubin, requires multiple chains
#Approximate convergence diagnosed when upper limit is close to 1.
gelman.diag(d)
gelman.plot(d, ask=TRUE)









